<template>
  <div>
    <div class="m-panel">
      <div
        class="m-panel-top"
        @mouseenter="otherOpenColorBand"
        @mouseleave="otherCloseColorBand">
        <div class="m-panel-top-list">
          <div
            class="m-panel-top-list-color"
            v-for="colorband in colors[colorIndex].value"
            :key="colorband"
            :style="{ background: colorband }">
          </div>
        </div>
      </div>
      <div class="m-panel-center">
        <span>{{ colorRange }}</span>
      </div>
      <div
        v-show="selfShow || otherShow"
        class="m-panel-bottom"
        @mouseenter="selfOpenColorBand"
        @mouseleave="selfCloseColorBand">
        <div
          class="m-panel-bottom-list"
          v-for="item in colors"
          :key="item.label"
          @click="selectColorBand(item.label)">
          <div
            class="m-panel-bottom-list-color"
            v-for="colorband in item.value"
            :key="colorband"
            :style="{ background: colorband }">
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'color-band',
  props: {
    colorRange: {
      type: String,
      default: '0-10'
    },
    colors: {
      type: Array,
      default: () => {
        return [
          {
            label: 0,
            value: ['#006837', '#1a9850', '#66bd63', '#a6d96a', '#d9ef8b', '#ffffbf', '#fee08b', '#fdae61', '#f46d43', '#d73027', '#a50026']
          }
        ]
      }
    }
  },
  data() {
    return {
      colorIndex: 0,
      selfShow: false,
      otherShow: false
    }
  },
  methods: {
    otherOpenColorBand() {
      this.otherShow = true
    },
    otherCloseColorBand() {
      setTimeout(() => {
        if (!this.selfShow) {
          this.otherShow = false
        }
      }, 100)
    },
    selfOpenColorBand() {
      this.selfShow = true
    },
    selfCloseColorBand() {
      this.selfShow = false
      this.otherShow = false
    },
    selectColorBand(val) {
      this.selfCloseColorBand()
      this.colorIndex = val
      this.$emit('onColorChange', val)
    }
  }
}
</script>

<style lang="scss" scoped>
.m-panel {
  position: relative;
  z-index: 1;
  height: 30px;

  &-top {
    position: absolute;
    width: 150px;
    background-color: #fff;
    border: 1px solid #E4E7ED;
    border-radius: 3px;

    &-list {
      display: flex;
      height: 15px;
      margin: 5px 0;
      padding: 0 10px;

      &-color {
        flex: 1;
      }
    }

  }

  &-center {
    position: absolute;
    left: 180px;
    padding: 5px 0;
  }

  &-bottom {
    position: absolute;
    top: 35px;
    width: 150px;
    overflow: hidden;
    background-color: #fff;
    border: 1px solid #E4E7ED;
    border-radius: 3px;

    &-list {
      display: flex;
      height: 15px;
      margin: 5px 0;
      padding: 0 10px;

      &:hover {
        background-color: #f0f0f0;
        cursor: pointer;
      }

      &-color {
        flex: 1;
      }
    }
  }
}
</style>
